﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VCommons.Http;

namespace Web.Core
{
    /// <summary>
    /// cookies方式序列化
    /// </summary>
    public class CookiesSerialization : ISerialization
    {
        #region Fields & Consts
        /// <summary>
        /// 支持本地调试，cookies域名
        /// </summary>
        static string cookiesDomain
        {
            get
            {
                if ("localhost".Equals(System.Web.HttpContext.Current.Request.Url.Host, StringComparison.CurrentCultureIgnoreCase))
                {
                    return "localhost";
                }
                return "www.eee114.com";
            }
        }

        /// <summary>
        /// cookies键值
        /// </summary>
        const string COOKIESNAME = "UserInfo";
        #endregion

        #region Properties
        /// <summary>
        /// 返回userid
        /// </summary>
        public int UserID
        {
            get
            {
                int _userid;
                int.TryParse(VCommons.Encryptor.Utility.DecryptString(CookieHelper.Read(COOKIESNAME, "UserID")), out _userid);
                return _userid;
            }

        }
        /// <summary>
        /// 返回username
        /// </summary>
        public string UserName
        {
            get
            {
                return CookieHelper.Read(COOKIESNAME, "UserName");
            }
        }
        /// <summary>
        /// 返回email
        /// </summary>
        public string Email
        {
            get
            {
                return CookieHelper.Read(COOKIESNAME, "Email");
            }
        }
        /// <summary>
        /// 用户是否处于登陆状态
        /// </summary>
        public bool IsLogin
        {
            get
            {
                return !string.IsNullOrWhiteSpace(UserName);
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// 登陆信息持久化,机制httpCookies
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="username"></param>
        /// <param name="email"></param>
        /// <param name="isSave"></param>
        public void Serialization(int userid, string username, string email, bool isSave)
        {
            Write("UserID", VCommons.Encryptor.Utility.EncryptString(userid.ToString()), isSave);
            Write("UserName", username, isSave);
            Write("Email", email, isSave);
        }
        /// <summary>
        /// 登陆信息持久化,机制httpCookies
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="username"></param>
        /// <param name="email"></param>
        public void Serialization(int userid, string username, string email)
        {
            Serialization(userid, username, email, false);
        }
        /// <summary>
        /// 清除cookies
        /// </summary>
        public void Clear()
        {
            CookieHelper.Remove(COOKIESNAME);
        }
        #endregion

        #region Write Cookies
        static void Write(string uik, object obj)
        {
            Write(uik, obj, true);
        }
        static void Write(string uik, object obj, bool isSave)
        {
            if (obj == null)
            {
                return;
            }
            if (isSave)
            {
                VCommons.Http.CookieHelper.WriteWithDomain(COOKIESNAME
                    , uik
                    , obj.ToString().Trim()
                    , 525600
                    , cookiesDomain
                );
            }
            else
            {
                VCommons.Http.CookieHelper.WriteWithDomain(COOKIESNAME
                     , uik
                     , obj.ToString().Trim()
                     , cookiesDomain
                 );
            }
        }
        #endregion
    }
}
